//
//  ViewController.m
//  ios13DemoTest
//
//  Created by hpkj on 2020/1/15.
//  Copyright © 2020 hpkj. All rights reserved.
//

#import "ViewController.h"
#import "HPFileManager.h"

//重定义移除缓存key-value
#define REMOVEDEFAULT(key)  [[NSUserDefaults standardUserDefaults] removeObjectForKey:key];\
[[NSUserDefaults standardUserDefaults] synchronize];

//重定义缓存存储key-value
#define SAVEDEFAULTS(value,key) [[NSUserDefaults standardUserDefaults] setObject:value forKey:key];\
[[NSUserDefaults standardUserDefaults] synchronize];
//根据key获取缓存内容key-value
#define GETDEFAULTS(key) [[NSUserDefaults standardUserDefaults] objectForKey:key]


static NSString *const HP_LocaCache_BasicDir = @"HP_LocaCache_BasicDir";//!<缓存根目录

static NSString *const HP_DetailPath = @"HP_DetailPath";//!<详细目录缓存

//真机调试模式下，打印一下HP_DetailPath的值，用命令行vim 接路径可以查看文件内容


@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
//    self.view.backgroundColor = [UIColor cyanColor];
//
//    UITextView * tv1 = [[UITextView alloc] initWithFrame:CGRectMake(0, 88, 200, 200)];
//    tv1.text = @"[[UITextView alloc] initWithFrame:CGRectMake(0, 0, 200, 200)]";
//        tv1.backgroundColor = [UIColor whiteColor];
//
//
//    UITextView * tv2 = [[UITextView alloc] initWithFrame:CGRectMake(0, 288 + 10, 200, 200)];
//    tv2.text = @"[[UITextView alloc] initWithFrame:CGRectMake(0, 200, 200, 200)]";
////    tv2.backgroundColor = [UIColor cyanColor];
//    tv2.backgroundColor = [UIColor whiteColor];
//
//
//    [self.view addSubview:tv1];
//    [self.view addSubview:tv2];
    
    UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(0, 100, 100, 60);
    button.backgroundColor = [UIColor cyanColor];
    [button setTitle:@"存字典" forState:(UIControlStateNormal)];
    [button addTarget:self action:@selector(savedic) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];
    
    UIButton * button1 = [UIButton buttonWithType:UIButtonTypeCustom];
    button1.frame = CGRectMake(0, 220, 100, 60);
    button1.backgroundColor = [UIColor cyanColor];
    [button1 setTitle:@"取字典" forState:(UIControlStateNormal)];
    [button1 addTarget:self action:@selector(prinDic) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button1];
    
}


-(void)savedic
{
    
    NSDictionary * dic = @{@"key":@"value"};
    //1.2存数据
    NSString * basicCachePath = GETDEFAULTS(HP_LocaCache_BasicDir);
    //根路径文件夹directoryName，可自己修改
    NSString* directoryName = @"basicDir";
    NSString* cachePath = [HPFileManager cacheDirectory];
    BOOL isDirectory;
    BOOL result = [HPFileManager fileExistInPath:basicCachePath isDirectory:&isDirectory];
    if (result) {
    }else
    {
        BOOL createResult = [HPFileManager createDirectoryInPath:cachePath directoryName:directoryName];
        if (createResult) {
            NSString * path = [NSString stringWithFormat:@"%@/%@",cachePath,directoryName];
            basicCachePath = path;
            SAVEDEFAULTS(path, HP_LocaCache_BasicDir)
        }else
        {
            NSLog(@"创建文件夹失败!");
            return;
        }
    }
    BOOL isi;
    BOOL fileExist1 = [HPFileManager fileExistInPath:basicCachePath isDirectory:&isi];
    NSString * fileUsePath;
//    json文件名字，这里用的是name,则生成的文件名字就是name.json
    NSString * name = @"name";
    NSString * fileName = [NSString stringWithFormat:@"%@.json",name];
    if (fileExist1 && isi) {
        NSString * filePath = [basicCachePath stringByAppendingPathComponent:fileName];
        fileUsePath = filePath;
    }
    SAVEDEFAULTS(fileUsePath, HP_DetailPath);
    [HPFileManager saveDictionary:dic isPlistFileOfPath:fileUsePath];
    
}

-(void)prinDic
{
    NSString * path = GETDEFAULTS(HP_DetailPath);
    //真机调试模式下，打印一下path的值，用命令行vim 接路径可以查看文件内容
    NSDictionary * dic = [HPFileManager dictionaryInPlistFileOfPath:path];
    NSLog(@"%@",dic);
    
}

@end

